<?php

function mdInsertRealEstate( $data ){ 
    global $user;       
    $group_price = findPrice2( $data['pr_txt_total_price'] );
    $group_area = findPrice2( $data['pr_txt_area'] );
    $rid = db_insert('tbnd_post_realestate')
            ->fields(array(
                'uid'                  =>  $user->uid,               
                'alias'            =>  '' , //$data['url_alias'] . '-id' . $tid . '.html' , //$url_alias ,
                'title'                =>  $data['pr_txt_title'] ,
                'content'              =>  nl2br(strip_tags($data['pr_tar_decription'])) , //nl2br(strip_tags($data['content'])) ,
                'type_post'            =>  $data['pr_slb_type_post'],				
                'type_real'            =>  $data['pr_slb_type_real'],		
                'id_city'              =>  $data['pr_slb_city'],
				'name_city'            =>  mdGetCityById( $data['pr_slb_city'] )->name ,
                'id_district'          =>  $data['pr_slb_district'],
				'name_district'        =>  mdGetDistrictById( $data['pr_slb_district'] )->name ,
                'id_ward'              =>  $data['pr_slb_ward'] ? $data['pr_slb_ward'] : 0,
				'name_ward'            =>  $data['pr_slb_ward'] ? mdGetWardById( $data['pr_slb_ward'] )->name : '',
				'id_street'            =>  $data['pr_slb_street'] ? $data['pr_slb_street'] : 0 ,
				'name_street'          =>  $data['pr_slb_street'] ? mdGetStreetById( $data['pr_slb_street'] )->name : '',
                'number_home'          =>  $data['pr_txt_address'] ? $data['pr_txt_address'] : '',
                'full_address'         =>  $data['pr_full_address'] ? $data['pr_full_address'] : '' ,                 
				'latitude'			   =>  $data['pr_txt_latitude'] ? $data['pr_txt_latitude'] : 0,
				'longitude'			   =>  $data['pr_txt_longitude'] ? $data['pr_txt_longitude'] : 0 ,                
                'project'              =>  $data['pr_slb_project'] ? $data['pr_slb_project'] : 0 ,
				'name_project'         =>  $data['pr_slb_project'] ? mdGetProjectById( $data['pr_slb_project'] )->name : '',
                'area'                 =>  $data['pr_txt_area'] ? $data['pr_txt_area'] : 0,
                'group_area'           =>  $group_area > 0 ? $group_area['value'] : 0 ,                
                'price'                =>  $data['pr_txt_total_price'] ? $data['pr_txt_total_price'] : 0 ,
                'group_price'          =>  $group_price > 0 ? $group_price['value'] : 0,
                'text_price'           =>  $data['pr_txt_text_price'] ? $data['pr_txt_text_price'] : 'Thỏa thuận',//convertPrice($priceVND , $data['realestate_unit'] , $data['id_type_currency'])  , //convertPrice($priceVND , $data['realestate_unit']) , 
                'legal'                =>  $data['pr_slb_legal'] > 0 ? $data['pr_slb_legal'] : 0 ,
                'direction'            =>  $data['pr_slb_direction'] > 0 ? $data['pr_slb_direction'] : 0,
                'front'                =>  $data['pr_txt_front'] > 0 ? $data['pr_txt_front'] : 0 ,
                'intake'                =>  $data['pr_txt_intake'] > 0 ? $data['pr_txt_intake'] : 0 ,
                'floor'                =>  $data['pr_txt_floor'] > 0 ? $data['pr_txt_floor'] : 0 ,
                'room'                 =>  $data['pr_txt_room'] > 0 ? $data['pr_txt_room'] : 0,
                'toilet'               =>  $data['pr_txt_toilet'] > 0 ? $data['pr_txt_toilet'] : 0, 
                'id_album'             =>  $data['id_album'] ? $data['id_album'] : 0,
                'total_img'			   =>  $data['total_img'] > 0 ? $data['total_img'] : 0 , 
				'm_fid'		       	   =>  $data['m_fid'] > 0 ? $data['m_fid'] : 0 ,                                 
                'start_date'           =>   REQUEST_TIME ,
                'update_date'          =>   REQUEST_TIME ,
                'end_date'             =>   REQUEST_TIME + 259200,
                'status'            =>   NOT_ACTIVED ,
            ))
            ->execute();
     /* Insert alias for realestate */
     $alias_url = trim(  str_utf8($data['pr_txt_title']) . '-r' . $rid . '.html' );
     db_update('tbnd_post_realestate')
        ->fields(array(                        
                'alias'  =>  $alias_url , 
            ))
            ->condition('rid', $rid)
            ->execute();
      $alias_path = array(
        'pid' => NULL,
        'language' => LANGUAGE_NONE,
        'source' => 'bat-dong-san/' . $rid ,
        'alias' => $alias_url
      );
      path_save( $alias_path );
     /*---------------------------------------*/
     
     /* Insert info contact for realestate */
     $cr_id = db_insert('tbnd_info_contact_realestate')
            ->fields(array(
                'rid'                  =>  $rid     ,   
                'address'              =>  $data['pr_txt_address_contact'] ?  $data['pr_txt_address_contact'] : '' ,
                'name'                 =>  $data['txt_full_name_contact'] ?  $data['txt_full_name_contact'] : '' ,
                'email'                =>  $data['txt_email_contact'] ?  $data['txt_email_contact'] : '' ,
                'tel'                  =>  $data['txt_tel_contact'] ?  $data['txt_tel_contact'] : '' ,
                'phone'                  =>  $data['txt_phone_contact'] ?  $data['txt_phone_contact'] : '' ,
                ))
            ->execute();
     /*----------------------------------------*/    
}

function mdGetCity(){
	$sql = "SELECT * FROM {provine} ORDER BY priority  DESC";
	$row = db_query( $sql)->fetchAll();
    return $row;
}


function mdGetCityById( $id ){
	if($id > 0){
		$sql = "SELECT * FROM {provine} WHERE id = $id";
		$row = db_query( $sql)->fetchObject();
		return $row;
	}
	return NULL;
}

function mdGetListTypeRealByTypePost( $type_post ){
	if( $type_post > 0){
		$sql = "SELECT * FROM {type_real} WHERE type_post = $type_post";
		$rows = db_query( $sql)->fetchAll();
		return $rows;
	}
	return NULL;
}

function mdGetTypeRealById( $id ){
	if($id > 0){
		$sql = "SELECT * FROM {type_real} WHERE id = $id";
		$rows = db_query( $sql)->fetchObject();
		return $rows;
	}
	return NULL;
}

function mdGetDistrictById( $id ){
	if($id){
		$sql = "SELECT * FROM {district} WHERE id = $id";
		$row = db_query( $sql)->fetchObject();
		return $row;
	}
	return NULL;
}

function mdGetListDistrictByCity( $city ){
	if($city > 0){
		$sql = "SELECT * FROM {district} WHERE provine=:provine ORDER BY name";
		$row = db_query( $sql , array(':provine' => $city))->fetchAll();
		return $row;
	}
	return NULL;
}

function mdGetWardById( $id ){
	if($id > 0){
		$sql = "SELECT * FROM {ward} WHERE id = $id";
		$row = db_query( $sql)->fetchObject();
		return $row;
	}
	return NULL;
}

function mdGetListWardByDistrict( $district ){
	if($district > 0){
		$sql = "SELECT * FROM {ward} WHERE district=:district ORDER BY name";
		$row = db_query( $sql , array(':district' => $district))->fetchAll();
		return $row;
	}
	return NULL;
}


function mdGetStreetById( $id ){
	if($id > 0){
		$sql = "SELECT * FROM {street} WHERE id = $id";
		$row = db_query( $sql)->fetchObject();
		return $row;
	}
	return NULL;
}

function mdGetListStreetByDistrict( $district ){
	if($district > 0){
		$sql = "SELECT * FROM {street} WHERE district=:district ORDER BY name";
		$row = db_query( $sql , array(':district' => $district))->fetchAll();
		return $row;
	}
	return NULL;
}

function mdGetProjectById( $id ){
	if($id > 0){
		$sql = "SELECT * FROM {project} WHERE id = $id";
		$row = db_query( $sql)->fetchObject();
		return $row;
	}
	return NULL;
}

function mdGetListProjectByDistrict( $district ){
	if($district > 0){
		$sql = "SELECT * FROM {project} WHERE district=:district ORDER BY name";
		$row = db_query( $sql , array(':district' => $district))->fetchAll();
		return $row;
	}
	return NULL;
}

function mdGetListLegalReal(){
	return array(
		'0' => array(
			'id' => 1, 
			'name' => 'Không xác định' , 
		),
		'1' => array(
			'id' => 2, 
			'name' => 'Sổ hồng' , 
		),
		'2' => array(
			'id' => 3, 
			'name' => 'Giấy đỏ' , 
		),
		'3' => array(
			'id' => 4, 
			'name' => 'Giấy tay' , 
		),
		'4' => array(
			'id' => 5, 
			'name' => 'Đang hợp thức hóa' , 
		),
		'5' => array(
			'id' => 6, 
			'name' => 'Giấy tờ hợp lệ' , 
		),
		'6' => array(
			'id' => 7, 
			'name' => 'Chủ quyền tư nhân' , 
		),
		'7' => array(
			'id' => 8, 
			'name' => 'Hợp đồng' , 
		),
	);
}

function mdGetNameLegalById( $id ){
    switch( $id ){
        case 1:
            return 'Không xác định';
            break;
        case 2:
            return 'Sổ hồng';
            break;
        case 3:
            return 'Giấy đỏ';
            break;
        case 4:
            return 'Giấy tay';
            break;
        case 5:
            return 'Đang hợp thức hóa';
            break;
        case 6:
            return 'Giấy tờ hợp lệ';
            break;
        case 7;
            return 'Chủ quyền tư nhân';
            break;
        case 8:            
            return 'Hợp đồng';
            break;
    }
    return 'Không xác định'; 
}

function mdGetListDirectionReal(){
	return array(
		'0' => array(
			'id' => 1, 
			'name' => 'Không xác định' , 
		),
		'1' => array(
			'id' => 2, 
			'name' => 'Đông' , 
		),
		'2' => array(
			'id' => 3, 
			'name' => 'Tây' , 
		),
		'3' => array(
			'id' => 4, 
			'name' => 'Nam' , 
		),
		'4' => array(
			'id' => 5, 
			'name' => 'Bắc' , 
		),
		'5' => array(
			'id' => 6, 
			'name' => 'Đông-Bắc' , 
		),
		'6' => array(
			'id' => 7, 
			'name' => 'Tây-Bắc' , 
		),
		'7' => array(
			'id' => 8, 
			'name' => 'Tây-Nam' , 
		),
		'8' => array(
			'id' => 9, 
			'name' => 'Đông-Nam' , 
		),
	);
}

function mdGetNameDirectionById( $id ){
    switch( $id ){
        case 1:
            return 'Không xác định';
            break;
        case 2:
            return 'Đông' ; 
            break;
        case 3:
            return 'Tây';
            break;
        case 4:
            return 'Nam';
            break;
        case 5:
            return 'Bắc';
            break;
        case 6:
            return 'Đông-Bắc';
            break;
        case 7;
            return 'Tây-Bắc';
            break;
        case 8:            
            return 'Tây-Nam';
            break;
        case 9:            
            return 'Đông-Nam';
            break;
    }
    return 'Không xác định'; 
}

function mdGetTypePost( $id ){
    switch( $id ){
        case 1:
            return 'Nhà đất cho thuê';
            break;
        case 2:
            return 'Nhà đất bán' ; 
            break;
	}
	return 'Nhà đất bán';//'''Không xác định'; 
}

function mdGetAllListRealEstate($start , $limit){
	$sql = "SELECT * FROM tbnd_post_realestate ORDER BY update_date DESC";
    $total = db_query( $sql )->rowCount();
    $rows = db_query_range( $sql, $start , $limit )->fetchAll();	
    return array(
		'data' => $rows,
        'total' => $total
    );
}

function mdSearchRealestate($data , $start , $limit){
    $sql_select = 'SELECT *';
	$sql = ' FROM tbnd_post_realestate  WHERE status = 0 AND 1 = 1';  
	$link_s = 'tim-kiem-nha-dat?';//''BASE_URL . 'tim-kiem-nha-dat?';
	$title = '';
	/*-------------array_breadcrumb----------------------*/
	$breadcrumb = array();
	/*-------------------------------------------------------------*/
	/*---------------check_value_type_post_-----------------------*/
	/*if($data['slb_type_post'] <= 0){
		$data['slb_type_post'] = 2;//mac dinh la ban
	}*/
	if($data['slb_type_post'] > 0){
		$sql.= ' AND type_post = ' . $data['slb_type_post'];
		$link_s .= 'slb_type_post=' . $data['slb_type_post'];
		//$title .= mdGetTypePost( $data['slb_type_post'] );		
	}
	else{
		$link_s .= 'slb_type_post=0';
	}
	/*------------------------------------------------------------*/
	
	/*---------------check_value_slb_type_real-----------------------*/
	if($data['slb_type_real'] > 0){
		$sql.= ' AND type_real = ' . $data['slb_type_real'];
		$n_type_real = mdGetTypeRealById( $data['slb_type_real'] )->name;
		$title .= ' ' . $n_type_real;
		$link_s .= '&slb_type_real=' . $data['slb_type_real'];
		$breadcrumb[0] = array(
            'index' => 0 , 
			'name' => $n_type_real ,
			'href' => BASE_URL . $link_s
		);
	}
	else{
        $n_type_post = mdGetTypePost( $data['slb_type_post'] );	
		$title .= $n_type_post;
		$link_s .= '&slb_type_real=' . $data['slb_type_real'];
        $breadcrumb[0] = array(
            'index' => 0 , 
			'name' => $n_type_post ,
			'href' => BASE_URL . $link_s
		);
	}
	/*------------------------------------------------------------*/
	
	
	/*---------------check_value_slb_project-----------------------*/
	if($data['slb_project'] > 0){		 
		$sql.= ' AND project = ' . $data['slb_project'];
        $n_project = mdGetProjectById( $data['slb_project'] )->name;
        $title .= ' trong dự án ' . $n_project;
        $link_s .= '&slb_project=' . $data['slb_project'];
        $breadcrumb[5] = array(
            'index' => 5 , 
			'name' => ' Trong dự án ' . $n_project ,
			'href' => BASE_URL . $link_s
		);
        
	}
	/*------------------------------------------------------------*/
    
	/*---------------check_value_slb_street-----------------------*/
	if($data['slb_street'] > 0){		 
		$sql.= ' AND id_street = ' . $data['slb_street']; 
		$n_street = mdGetStreetById( $data['slb_street'] )->name;
		$title .= ' ' . $n_street;
		$link_s .= '&slb_street=' . $data['slb_street'];
		$breadcrumb[4] = array(
            'index' => 4 , 
			'name' => $n_street ,
			'href' => BASE_URL . $link_s
		);
	}
	/*------------------------------------------------------------*/
	
	
	/*---------------check_value_slb_ward-----------------------*/
	if($data['slb_ward'] > 0){		 
		$sql.= ' AND id_ward = ' . $data['slb_ward'];
		$n_ward = mdGetWardById( $data['slb_ward'] )->name;
		$title .= ' ' . $n_ward;
		$link_s .= '&slb_ward=' . $data['slb_ward'];
		$breadcrumb[3] = array(
            'index' => 3 , 
			'name' => $n_ward ,
			'href' => BASE_URL . $link_s
		);
	}
	/*------------------------------------------------------------*/
	
	
	/*---------------check_value_slb_district-----------------------*/
	if($data['slb_district'] > 0){
		$sql.= ' AND id_district = ' . $data['slb_district'];
		$n_district = mdGetDistrictById( $data['slb_district'] )->name;
		$title .= ' ' . $n_district;
		$link_s .= '&slb_district=' . $data['slb_district'];
		$breadcrumb[2] = array(
            'index' => 2 , 
			'name' => $n_district ,
			'href' => BASE_URL . $link_s
		);
	}
	/*------------------------------------------------------------*/
	
	
	/*---------------check_value_slb_city-----------------------*/
	if($data['slb_city'] > 0){
		$sql.= ' AND id_city = ' . $data['slb_city'];
		$n_city = mdGetCityById( $data['slb_city'] )->name;
		$title .= ' ' . $n_city;
		$link_s .= '&slb_city=' . $data['slb_city'];
		$breadcrumb[1] = array(
            'index' => 1 , 
			'name' => $n_city ,
			'href' => BASE_URL . $link_s
		);
	}
	else{
		$title .= ' cả nước ';
	}
	/*------------------------------------------------------------*/
	
	
	/*---------------check_value_slb_price-----------------------*/
	if($data['slb_price'] > 0){
		$price = aSearchPrice(); 
		$price = $price[$data['slb_price']]; 
		$sql.= ' AND (price <= '. $price['max_price'] . ' AND price >= ' . $price['min_price'] . ') ';		
	}
    
    /*-------------------Statistic_by_price--------------------------*/
     $list_price = mdGetListPriceByTypePost( $data['slb_type_post'] ); 
     $sql_select_static_price = 'SELECT group_price , count(*) as total';
        $sql_statistic_price = $sql_select_static_price . $sql . ' GROUP BY group_price';
       $list_statistic_price = db_query( $sql_statistic_price )->fetchAll();
     /*foreach ($list_price as $sta_price){
       $sql_statistic_price = $sql . ' AND (price <= '. $sta_price['max_price'] . ' AND price >= ' . $sta_price['min_price'] . ') ';	
       $total_statistic = db_query( $sql_statistic_price )->rowCount();
       if($total_statistic > 0){
            $list_statistic_price[] =  array(
                'price' => $sta_price ,
                'total' => $total_statistic , 
            );
       }
     }      */      
     /*------------------------------------------------------------*/
	/*------------------------------------------------------------*/
	
	/*---------------check_value_slb_area-----------------------*/
	if($data['slb_area'] > 0){
		$area = aSearchArea(); 
		$area = $area[$data['slb_area']]; 
		$sql.= ' AND (area <= '. $area['max_area'] . ' AND area >= ' . $area['min_area'] . ') ';		
	}
    
     /*-------------------Statistic_by_area--------------------------*/   
    $sql_select_static_area = 'SELECT group_area , count(*) as total';
    $sql_statistic_area = $sql_select_static_area . $sql . ' GROUP BY group_area';
    $list_statistic_area = db_query( $sql_statistic_area )->fetchAll();
     
    /*$list_area = aSearchArea( );       
     foreach ($list_area as $sta_area){
       $sql_statistic_area = $sql . ' AND (area <= '. $sta_area['max_area'] . ' AND area >= ' . $sta_area['min_area'] . ') ';	
       $total_statistic = db_query( $sql_statistic_area )->rowCount();
       if($total_statistic > 0){
            $list_statistic_area[] =  array(
                'area' => $sta_area ,
                'total' => $total_statistic , 
            );
       }
     } */           
     /*------------------------------------------------------------*/
	/*------------------------------------------------------------*/
	
	/*---------------check_value_slb_direction-----------------------*/
	if($data['slb_direction'] > 0){		 
		$sql.= ' AND direction = ' . $data['slb_direction'];
	}
	/*------------------------------------------------------------*/
	
	/*---------------check_value_slb_floor-----------------------*/
	if($data['slb_floor'] > 0){		 
		$sql.= ' AND floor >= ' . $data['slb_floor'];
	}
	/*------------------------------------------------------------*/
	
	/*---------------check_value_slb_room-----------------------*/
	if($data['slb_room'] > 0){		 
		$sql.= ' AND room >= ' . $data['slb_room'];
	}
	/*------------------------------------------------------------*/
	
	/*---------------check_value_slb_toilet-----------------------*/
	if($data['slb_toilet'] > 0){		 
		$sql.= ' AND toilet >= ' . $data['slb_room'];
	}
	/*------------------------------------------------------------*/
	
	/*---------------check_value_slb_legal-----------------------*/
	if($data['slb_legal'] > 0){		 
		$sql.= ' AND legal = ' . $data['slb_legal'];
	}
	/*------------------------------------------------------------*/
	
	/*---------------check_value_slb_front-----------------------*/
	if($data['slb_front'] > 0){		 
		$sql.= ' AND front >= ' . $data['slb_front'];
	}
	/*------------------------------------------------------------*/
	
	/*---------------order_by-----------------------------------*/
	$sql .=  ' ORDER BY update_date DESC';
	/*-----------------------------------------------------------*/
	
	$total = db_query( $sql_select . $sql )->rowCount();
    $rows = db_query_range( $sql_select . $sql , $start , $limit )->fetchAll();	
    return array(
		'title' => $title , 
		'breadcrumb' => $breadcrumb , 
        'list_statistic_price' => $list_statistic_price ? $list_statistic_price : NULL , 
        'list_statistic_area' =>  $list_statistic_area ? $list_statistic_area : NULL , 
        'link_s' => $link_s , 
		'data' => $rows,
        'total' => $total
    );
}


function mdGetListRealVipByParam($start , $limit){
	$sql = "SELECT * FROM tbnd_post_realestate ORDER BY update_date DESC";   
    $rows = db_query_range( $sql, $start , $limit )->fetchAll();	
	return $rows;
}

function mdGetInfoRealEstateById( $id ){
	$sql = "SELECT * FROM tbnd_post_realestate WHERE rid = $id";    
    $row = db_query( $sql )->fetchObject();	
    return mdGetAllInfoRealEstateByObject( $row );
}
function mdGetAllInfoRealEstateByObject( $row ){
    if($row){
		$row->main_image = api_file_load($row->m_fid);
		$row->link = checkAndInsertAliasPathRealEstate( 'bat-dong-san/' . $row->rid , str_utf8($row->title) . '-r' . $row->rid . '.html'   );
        $row->list_images = mdGetListImagesByAid( $row->id_album );       
        $row->info_contact = mdGetInfoContactRealestateByRid( $row->rid );
        $row->legal = mdGetNameLegalById( $row->legal );
		return $row;
	}
	return NULL;
}
function mdGetInfoRealEstateByObject( $row ){
	if($row){
		$row->main_image = api_file_load($row->m_fid); 
        $row->link = checkAndInsertAliasPathRealEstate( 'bat-dong-san/' . $row->rid , str_utf8($row->title) . '-r' . $row->rid . '.html'   );//drupal_get_path_alias('bat-dong-san/' . $row->rid);//checkAndInsertAliasPathRealEstate( 'bat-dong-san/' . $row->rid , str_utf8($row->title) . '-r' . $row->rid . '.html'   );//drupal_get_path_alias('bat-dong-san/' . $row->rid);
		return $row;
	}
	return NULL;
}

function mdGetInfoContactRealestateByRid( $rid ){
    $sql = "SELECT * FROM tbnd_info_contact_realestate WHERE rid = $rid";    
    $row = db_query( $sql )->fetchObject();	
    return $row;
}

function mdStatisticProductByPrice( $s ){
    switch( $s['slb_type_post'] ){
        case 1://cho thue
            
            break;
        default :
            $list_price = aSearchSalePrice(); 
            foreach ($list_price as $price){
                $total = mdTotalProductByPrice($s , $price['min_price'] , $price['max_price']);
                if($total > 0){
                    $data[] = array(
                        'title' => '' ,
                        'total' => $total , 
                    ); 
                }
            }            
            break;
    }
    return $data;
}

function mdTotalProductByPrice( $data ){
    $sql = 'SELECT group_price , count(*) as total FROM tbnd_post_realestate  WHERE status = 0 AND 1 = 1';     
	/*---------------check_value_type_post_-----------------------*/
	/*if($data['slb_type_post'] <= 0){
		$data['slb_type_post'] = 2;//mac dinh la ban
	}*/
	if($data['slb_type_post'] > 0){
		$sql.= ' AND type_post = ' . $data['slb_type_post'];
		//$title .= mdGetTypePost( $data['slb_type_post'] );		
	}
	/*------------------------------------------------------------*/
	
	/*---------------check_value_slb_type_real-----------------------*/
	if($data['slb_type_real'] > 0){
		$sql.= ' AND type_real = ' . $data['slb_type_real'];		
	}	
	/*------------------------------------------------------------*/
	
	
	/*---------------check_value_slb_project-----------------------*/
	if($data['slb_project'] > 0){		 
		$sql.= ' AND project = ' . $data['slb_project'];       
	}
	/*------------------------------------------------------------*/
    
	/*---------------check_value_slb_street-----------------------*/
	if($data['slb_street'] > 0){		 
		$sql.= ' AND id_street = ' . $data['slb_street']; 		
	}
	/*------------------------------------------------------------*/
	
	
	/*---------------check_value_slb_ward-----------------------*/
	if($data['slb_ward'] > 0){		 
		$sql.= ' AND id_ward = ' . $data['slb_ward'];		
	}
	/*------------------------------------------------------------*/
	
	
	/*---------------check_value_slb_district-----------------------*/
	if($data['slb_district'] > 0){
		$sql.= ' AND id_district = ' . $data['slb_district'];		
	}
	/*------------------------------------------------------------*/
	
	
	/*---------------check_value_slb_city-----------------------*/
	if($data['slb_city'] > 0){
		$sql.= ' AND id_city = ' . $data['slb_city'];		
	}
	
	/*------------------------------------------------------------*/
	
	
	/*---------------check_value_slb_price-----------------------*/
	//$sql.= ' AND (price <= '. $max_price . ' AND price >= ' . $min_price . ') ';		
    $sql .= ' GROUP BY group_price';
	/*------------------------------------------------------------*/	
	$rows = db_query( $sql )->fetchAll();//rowCount();
    return $rows;
}


function mdTotalProductByArea( $data ){
    $sql = 'SELECT group_area , count(*) as total FROM tbnd_post_realestate  WHERE status = 0 AND 1 = 1';     
	/*---------------check_value_type_post_-----------------------*/
	/*if($data['slb_type_post'] <= 0){
		$data['slb_type_post'] = 2;//mac dinh la ban
	}*/
	if($data['slb_type_post'] > 0){
		$sql.= ' AND type_post = ' . $data['slb_type_post'];
		//$title .= mdGetTypePost( $data['slb_type_post'] );		
	}
	/*------------------------------------------------------------*/
	
	/*---------------check_value_slb_type_real-----------------------*/
	if($data['slb_type_real'] > 0){
		$sql.= ' AND type_real = ' . $data['slb_type_real'];		
	}	
	/*------------------------------------------------------------*/
	
	
	/*---------------check_value_slb_project-----------------------*/
	if($data['slb_project'] > 0){		 
		$sql.= ' AND project = ' . $data['slb_project'];       
	}
	/*------------------------------------------------------------*/
    
	/*---------------check_value_slb_street-----------------------*/
	if($data['slb_street'] > 0){		 
		$sql.= ' AND id_street = ' . $data['slb_street']; 		
	}
	/*------------------------------------------------------------*/
	
	
	/*---------------check_value_slb_ward-----------------------*/
	if($data['slb_ward'] > 0){		 
		$sql.= ' AND id_ward = ' . $data['slb_ward'];		
	}
	/*------------------------------------------------------------*/
	
	
	/*---------------check_value_slb_district-----------------------*/
	if($data['slb_district'] > 0){
		$sql.= ' AND id_district = ' . $data['slb_district'];		
	}
	/*------------------------------------------------------------*/
	
	
	/*---------------check_value_slb_city-----------------------*/
	if($data['slb_city'] > 0){
		$sql.= ' AND id_city = ' . $data['slb_city'];		
	}
	
	/*------------------------------------------------------------*/
	
	
	/*---------------check_value_slb_price-----------------------*/
	//$sql.= ' AND (price <= '. $max_price . ' AND price >= ' . $min_price . ') ';		
    $sql .= ' GROUP BY group_area';
	/*------------------------------------------------------------*/	
	$rows = db_query( $sql )->fetchAll();//rowCount();
    return $rows;
}

function mdSearchRelationshipDetailRealestate($rid , $data){
	 $sql = 'SELECT * FROM tbnd_post_realestate  WHERE status = 0 AND rid != ' . $rid;     
	/*---------------check_value_type_post_-----------------------*/
	/*if($data['slb_type_post'] <= 0){
		$data['slb_type_post'] = 2;//mac dinh la ban
	}*/
	if($data['slb_type_post'] > 0){
		$sql.= ' AND type_post = ' . $data['slb_type_post'];
		//$title .= mdGetTypePost( $data['slb_type_post'] );		
	}
	/*------------------------------------------------------------*/
	
	/*---------------check_value_slb_type_real-----------------------*/
	if($data['slb_type_real'] > 0){
		$sql.= ' AND type_real = ' . $data['slb_type_real'];		
	}	
	/*------------------------------------------------------------*/
	
	
	/*---------------check_value_slb_project-----------------------*/
	if($data['slb_project'] > 0){		 
		$sql.= ' AND project = ' . $data['slb_project'];       
	}
	/*------------------------------------------------------------*/
    
	/*---------------check_value_slb_street-----------------------*/
	if($data['slb_street'] > 0){		 
		$sql.= ' AND id_street = ' . $data['slb_street']; 		
	}
	/*------------------------------------------------------------*/
	
	
	/*---------------check_value_slb_ward-----------------------*/
	/*if($data['slb_ward'] > 0){		 
		$sql.= ' AND id_ward = ' . $data['slb_ward'];		
	}*/
	/*------------------------------------------------------------*/
	
	
	/*---------------check_value_slb_district-----------------------*/
	if($data['slb_district'] > 0){
		$sql.= ' AND id_district = ' . $data['slb_district'];		
	}
	/*------------------------------------------------------------*/
	
	
	/*---------------check_value_slb_city-----------------------*/
	if($data['slb_city'] > 0){
		$sql.= ' AND id_city = ' . $data['slb_city'];		
	}
	
	/*------------------------------------------------------------*/
	
	
	/*---------------check_value_slb_price-----------------------*/
	//$sql.= ' AND (price <= '. $max_price . ' AND price >= ' . $min_price . ') ';		
   // $sql .= ' GROUP BY group_area';
	$sql .= ' limit 10';
	/*------------------------------------------------------------*/	
	$rows = db_query( $sql )->fetchAll();//rowCount();
    return $rows;
}
